//
//  TabbarController.m
//  Theme
//
//  Created by 孙炎 on 15/11/12.
//  Copyright © 2015年 lanouhn. All rights reserved.
//

#import "SettingController.h"

@interface SettingController ()

@end

@implementation SettingController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"设置";
    
    self.view.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"ygColor"];
    
    _dataArray1=[[NSMutableArray alloc]initWithObjects:@"开启推送",@"开启声音",@"夜间模式",nil];
    _dataArray2=[[NSMutableArray alloc]initWithObjects:@"我的收藏",@"清理缓存",nil];
    _dataArray3=[[NSMutableArray alloc]initWithObjects:@"声明条款",nil];
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) style:UITableViewStylePlain];
    _myTableView.tableFooterView = [[UIView alloc]init];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.showsHorizontalScrollIndicator = NO;
    _myTableView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
    
    [self.view addSubview:_myTableView];

    //接收主题改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeThemeNotificantion) name:ThemeChangeNotification object:nil];
    
    [self changeThemeNotificantion];
    // Do any additional setup after loading the view.
}

//改变主题的通知
- (void)changeThemeNotificantion{
    
    self.view.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"ygColor"];
    _myTableView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
}

//指定标题的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    _myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 20)];
    _myView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
    return _myView;
}


//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

//指定每个分区中有多少行，默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return  _dataArray1.count;//每个分区通常对应不同的数组，返回其元素个数来确定分区的行数
            break;
        case 1:
            return  _dataArray2.count;
            break;
        case 2:
            return  _dataArray3.count;
            break;
        default:
            return 0;
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString*cellId=@"cellId";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
    
    //根据沙盒里的值来控制switch的开关
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [ user objectForKey:@"userPassWord"];
    if (!(passWord == nil) && [passWord isEqualToString:@"123456"]){
        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor blackColor];
    }else{
        cell.contentView.backgroundColor = RGB(4.0,43.0,85.0,1.0f);
        cell.textLabel.textColor = RGB(153.0,153.0,153.0,1.0f);
    }

    NSArray *array= cell.contentView.subviews;
    for (UIView *view in array ) {
        [view removeFromSuperview];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    if (indexPath.section==0) {
        cell.textLabel.text=[_dataArray1 objectAtIndex:indexPath.row];
        if (indexPath.row == 0){
        }else if(indexPath.row == 1){
        }else{
            _switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth-20-42, 15, 42, 22)];
            
            //根据沙盒里的值来控制switch的开关
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *passWord = [ user objectForKey:@"userPassWord"];
            if (!(passWord == nil) && [passWord isEqualToString:@"123456"]){
                [_switchButton setOn:NO];
            }else{
                [_switchButton setOn:YES];
            }
            
            _switchButton.tag=indexPath.row+10001;
            [_switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            
            [cell.contentView addSubview:_switchButton];
            
        }
        cell.selectionStyle = NO;
        cell.accessoryType = NO;
    }
    
    if (indexPath.section==1) {
        cell.textLabel.text=[_dataArray2 objectAtIndex:indexPath.row];
        UIImageView*arrowImageView=[[UIImageView alloc]init];;
        arrowImageView.frame=CGRectMake(screenWidth-13-11, (60-20)/2, 10, 18);
        arrowImageView.image=[UIImage imageNamed:@"arrow.png"];
        [cell.contentView addSubview:arrowImageView];
        
    }
    if (indexPath.section==2) {
        cell.textLabel.text=[_dataArray3 objectAtIndex:indexPath.row];
        UIImageView*arrowImageView=[[UIImageView alloc]init];;
        arrowImageView.frame=CGRectMake(screenWidth-13-11, (60-20)/2, 10, 18);
        arrowImageView.image=[UIImage imageNamed:@"arrow.png"];
        [cell.contentView addSubview:arrowImageView];

    }
    cell.selectionStyle = NO;
    cell.accessoryType = NO;

    
    return cell;
}

-(void)switchAction:(id)sender{

    //取出选中的主题名称
    NSString *themeName = [ConfigurationTheme shareInstance].themeName;
    
    if ([themeName isEqualToString:@"默认"]){
        themeName = @"nt";

        //往沙盒存个值
        NSString *passWord = @"11111";
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:passWord forKey:@"userPassWord"];
        
    }else{
        
        themeName = @"默认";
        //往沙盒存个值
        NSString *passWord = @"123456";
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:passWord forKey:@"userPassWord"];
    }
    
    [ConfigurationTheme shareInstance].themeName = themeName;
    [[NSNotificationCenter defaultCenter] postNotificationName:ThemeChangeNotification object:nil];
    //保存主题到本地
    [[NSUserDefaults standardUserDefaults] setObject:themeName forKey:ThemeName];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [_myTableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
